package com.edu.controller.announcement;

import com.alibaba.fastjson2.JSONObject;
import com.edu.dto.ResultDto;
import com.edu.entity.AnnouncementEntity;
import com.edu.service.AnnouncementsService;
import com.edu.service.impl.AnnouncementsServiceImpl;
import com.edu.utils.JwtUtils;
import com.edu.utils.ReturnCode;
import com.edu.utils.ServletHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
 * com.edu.controller.announcement
 * 2024/7/14 22:35
 *
 * @author Fancy
 * @version 1.0
 */
@WebServlet(urlPatterns = {"/announcements","/announcements/*"})
public class AnnouncementsController extends HttpServlet {
    AnnouncementsService announcementsService = new AnnouncementsServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        AnnouncementEntity announcement = ServletHelper.getRequestBodyAsObject(req, AnnouncementEntity.class);

        //从请求头获取token
        String token = req.getHeader("token");
        int row = announcementsService.createAnnouncement(announcement,token);


        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "添加失败,权限不足");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        List<AnnouncementEntity> announcements = announcementsService.readAllAnnouncements();


        if (announcements != null) {
            String success = ResultDto.success(announcements);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        int announcementsId = Integer.parseInt(req.getPathInfo().substring(1));

        JSONObject json = ServletHelper.getRequestBodyAsJson(req);
        String status = json.getString("status");

        //从请求头获取token
        String token = req.getHeader("token");

        int row = announcementsService.updateAnnouncementStatus(status,announcementsId,token);


        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "更新失败,权限不足");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }
}
